function deleteEquipment(id) {
    swal({
            title: "您确定要删除这条信息吗",
            text: "删除后将无法恢复，请谨慎操作！",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "是的，我要删除！",
            cancelButtonText: "让我再考虑一下…",
            closeOnConfirm: false,
            closeOnCancel: false
        },
        function (isConfirm) {
            if (isConfirm) {
                sendDeleteRequest(id),
                    swal("删除成功！", "您已经永久删除了这条信息。", "success");
            } else {
                swal("已取消", "您取消了删除操作！", "error");
            }
        });
}


/**
 * [[发送删除请求]]
 * @param {[[String]]} [[请求参数]]
 */
function sendDeleteRequest(id) {
    $.post(getRootPath()+"/equipment/delete/" + id, "", function (json) {
        if (json.code === 200) {
            Tips.showSuccess(json.msg);
            //定时刷新页面
            setTimeout(function () {
                window.location.reload();
            }, 2000);
        } else {
            Tips.showError(json.msg)
        }
    }, "json");
}


//随页面加载班级数据
$(function () {
    //发起请求，请求班级列表数据
    $.post(getRootPath()+"/equipment_list/categorylist", "", function (result) {
        //解析JSON格式数据
        if (result.code === 200) {
            //存储所有的option文本
            var class1_selectTxt = "";
            //遍历json数组   index  下标
            for (var index in result.data) {
                var class1 = result.data[index];
                class1_selectTxt += "<option  value='" + class1.id + "'>" + class1.type + "</option>";
            }
            $("#class3").append(class1_selectTxt)
            $("#class1").append(class1_selectTxt)

        }
    }, "json");
    $.post(getRootPath()+"/equipment_list/supplierlist", "", function (result) {
        //解析JSON格式数据
        if (result.code === 200) {
            //存储所有的option文本
            var supplier_selectTxt = "";
            //遍历json数组   index  下标
            for (var index in result.data) {
                var supplier = result.data[index];
                supplier_selectTxt += "<option  value='" + supplier.id + "'>" + supplier.ename+ "</option>";
            }
            $("#supplier3").append(supplier_selectTxt)
            $("#supplier1").append(supplier_selectTxt)

        }
    }, "json");
});




$(document).ready(function () {
    //#pirmary 是button的id  数据都根据各个文本框的id获取
    $('#primary').click(function () {
            var data = {
                name: $("#name3").val(),
                c_name: $("#class3").val(),
                price: $("#price3").val(),
                buydate: $("#deadline3").val(),
                number: $("#number3").val(),
                status: $("#status3").val(),
                parameter: $("#parameter3").val(),
                s_name: $("#supplier3").val(),
            }

            $.ajax({
                type: "post",
                url: getRootPath()+"/equipment/add",
                data: JSON.stringify(data),
                dataType: 'json',
                contentType: "application/json;charset=UTF-8",
                success: function (result) {
                    if (result.code == 200) {
                        swal({
                            title: "添加成功！",
                            type: "success"
                        });
                        setTimeout(function () {
                            window.location.reload();
                        }, 2000);
                    } else {
                        swal("添加失败", "编号必须唯一！");
                    }
                }
            })
        }
    );
});

//编辑回显数据
function update(id) {
    $.ajax({
        type: "post",
        "url": getRootPath()+"/equipment/findById/" + id,
        "data": "",
        "async": false,
        "dataType": "json",
        "success": function (result) {
            if (result.code == 200) {
                $("#id").val(result.data.id);
                $("#deadline1").val(result.data.buydate);
                $("#name1").val(result.data.name);
                $("#price1").val(result.data.price);
                $("#number1").val(result.data.number);
                $("#status1").val(result.data.status);
                $("#parameter1").val(result.data.parameter);

            } else {
                error.innerHTML = json.msg;
            }
        }
    });
}



//入住人员的编辑功能
function updateById(form) {
    $.ajax({
        type: "post",
        "url": getRootPath()+"/equipment/updateById",
        "data": "name1=" + $(form.name1).val() + "&classid=" + $(form.class1).val() + "&s_name=" + $(form.supplier1).val() + "&price=" + $(form.price1).val() + "&deadline=" + $(form.deadline1).val() +"&number=" + $(form.number1).val()+"&status=" + $(form.status1).val()+"&parameter=" + $(form.parameter1).val()+ "&id=" + $(form.id).val(),
        "async": false,
        "dataType": "json",
        "success": function (result) {
            if (result.code == 200) {
                swal({
                    title: "修改成功！",
                    type: "success"
                });
                setTimeout(function () {
                    window.location.reload();
                }, 3000);
            } else {
                swal({
                    title: "修改失败！",
                    type: "success"
                });
            }
        }
    });
    return false;
}


